﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wintellect.PowerCollections;
using System.Collections;

namespace PriorityQueue
{
    class PriorityQueue<T> : IEnumerable<T>
        where T : IComparable
    {
        private OrderedBag<T> orderedBag = null;

        public int Count
        {
            get { return this.orderedBag.Count; }
        }

        public bool IsEmpty
        {
            get
            {
                if (this.Count == 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public PriorityQueue()
        {
            this.orderedBag = new OrderedBag<T>();
        }

        public void Enqueue(T item)
        {
            this.orderedBag.Add(item);
        }

        public T Top()
        {
            if (this.IsEmpty == true)
            {
                throw new Exception("The queue is empty");
            }

            return this.orderedBag[0];
        }

        public T Dequeue()
        {
            if (this.IsEmpty == true)
            {
                throw new Exception("You cannot dequeue from empty queue");
            }

            T deletedValue = this.Top();
            this.orderedBag.RemoveFirst();
            return deletedValue;
        }

        public void Clear()
        {
            this.orderedBag = new OrderedBag<T>();
        }

        public T this[int index]
        {
            get
            {
                if (index < 0 || index >= this.Count)
                {
                    throw new IndexOutOfRangeException();
                }
                return this.orderedBag[index];
            }
        }

        public IEnumerator<T> GetEnumerator()
        {
            foreach (T item in this.orderedBag)
            {
                yield return item;
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    }
}
